#include "STC89C5xRC.H"
#include "led_loop.h"
#include "tim.h"
#include "task.h"

#if LED_LOOP_TASK

static volatile bool s_led_flash_status;
static volatile uint8_t s_led_count;
static volatile uint8_t s_led_num_table_index;
static volatile uint8_t s_led_num_table[11] = {0xC0, 0xF9, 0x89, 0XB0, 0x83, 0x92, 0x83, 0xF8, 0x80, 0x98, 0x00};

/**
 * @brief led_loop
 *
 * @param led_param
 */
static void led_loop(led_t *led_param)
{
    /* handle LED status start */
    switch (led_param->led_mode) /* chehck led loop mode */
    {
    case 0:
        /* handle LED mode 0 start */
        led_param->led_status = (led_param->led_status << 1) | 0x01;
        if (led_param->led_status == 0xFF)
        {
            led_param->led_status = 0xFF; /* reset led status */
            led_param->led_mode = 1;      /* set led loop mode_1 */
        }
        /* handle LED mode 0 end */
        break;
    case 1:
        /* handle LED mode 1 start */
        led_param->led_status = s_led_num_table[s_led_num_table_index++];
        if (s_led_num_table_index == 12)
        {
            s_led_num_table_index = 0;    /* clear table index */
            led_param->led_mode = 2;      /* set led loop mode_2 */
            led_param->led_status = 0xFF; /* reset led status */
        }
        /* handle LED mode 1 end */
        break;
    case 2:
        /* handle LED mode 2 start */
        s_led_flash_status = !s_led_flash_status;
        if (s_led_flash_status)
        {
            led_param->led_status = 0x00; /* set led status */
        }
        else
        {
            led_param->led_status = 0xFF; /* reset led status */
        }
        if ((s_led_count++) > 8)
        {
            s_led_count = 0;     /* clear led count */
            led_init(led_param); /* reset led status */
        }
        /* handle LED mode 2 end */
        break;
    default:
        break;
    }
    /* hanele LED status end */

    /* set LED output start */
    P1 = led_param->led_status;
    /* set LED output end */
}

/**
 * @brief led_init
 *
 * @param led_param
 */
void led_init(led_t *led_param)
{
    led_param->led_status = 0xFE;
    led_param->led_mode = 0;
}

/**
 * @brief led_loop_proc
 *
 * @param led_param
 */
void led_loop_proc(led_t *led_param)
{
    led_loop(led_param); /* run led loop */
}

#endif